//
//  APVideoDataManager.m
//  AqualPolo
//
//  Created by dong on 2024/5/12.
//

#import "APVideoDataManager.h"

@interface APVideoDataManager ()
@property (nonatomic, strong) NSArray *titles;
@end
@implementation APVideoDataManager

- (NSArray *)titles{
    if (!_titles) {
        _titles = @[
            @"💦 Dive into the thrill of water polo.",
            @"🚿 Making waves and scoring goals.",
            @"🤾 Polo passion in every throw.",
            @"💦 Be passionate about water polo.",
            @"🕺 Welcome to the world of water polo excitement!",
            @"🎉 Nothing beats the rush of scoring a goal in Water Polo!",
            @"🏊 Water Polo is not just a sport, it's a way of life!",
            @"🕰️ The precision and timing in Water Polo are essential for success.",
            @"💯 The competitiveness in Water Polo brings out the best in every player.",
            @"👫 The camaraderie among Water Polo players is unparalleled.",
            @"🤝 The teamwork in Water Polo is truly remarkable!",
            @"🤓 The strategy and skill required in Water Polo make it a favorite among athletes.",
            @"🔥 The excitement of Water Polo never gets old!",
            @"🏊 Water polo is an exciting sport!",
            @"🌍 The diversity of Water Polo players makes the sport even more exciting!",
        ];
    }
    return _titles;
}

+ (instancetype)shareInstance{
    static APVideoDataManager *shareId = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareId = [[self alloc] init];
    });
    return shareId;
}

- (NSMutableArray *)videosList{
    if (!_videosList) {
        _videosList = [NSMutableArray array];
    }
    return _videosList;
}

- (void)addVideo:(APVideoModel *)videoModel{
    if (self.videosList.count > 0) {
        [self.videosList insertObject:videoModel atIndex:0];
    }else{
        [self.videosList addObject:videoModel];
    }
    NSArray *result = [self.videosList yy_modelToJSONObject];
    [self saveInfo:result toFile:video_file];
}


- (void)updateVideo{
    if (self.videosList.count > 0) {
        NSArray *result = [self.videosList yy_modelToJSONObject];
        [self saveInfo:result toFile:video_file];
    }
}

- (void)blockUserVideos:(NSString *)userId{
    if (self.videosList.count >  0) {
        for (APVideoModel *model in self.videosList) {
            if ([model.userId isEqualToString:userId]) {
                model.isBlack = true;
            }
        }
    }
    NSArray *res = [self.videosList yy_modelToJSONObject];
     [self saveInfo:res toFile:video_file];
}

- (void)unBlockUserVideos:(NSString *)userId{
    if (self.videosList.count >  0) {
        for (APVideoModel *model in self.videosList) {
            if ([model.userId isEqualToString:userId]) {
                model.isBlack = false;
            }
        }
    }
    NSArray *res = [self.videosList yy_modelToJSONObject];
     [self saveInfo:res toFile:video_file];
}



- (void)initVideoData:(APVideoDataResultBlock)callBack{
    if (self.videosList.count > 0) {
        callBack(self.videosList);
        return;
    }
    
    NSArray *result = [self readFileDataAtPath:video_file];
     if (result && result.count > 0) {
         for (NSDictionary *dict in result) {
             APVideoModel *model = [APVideoModel yy_modelWithDictionary:dict];
             [self.videosList addObject:model];
         }
         callBack(self.videosList);
         return;
     }
    
   
    NSArray *users = [APUserDataManager shareInstance].users;
    [self makeVideoData:users result:callBack];
    if (self.videosList.count > 0) {
        NSArray *result = [self.videosList yy_modelToJSONObject];
        [self saveInfo:result toFile:video_file];
    }
}


- (void)makeVideoData:(NSArray *)users result:(APVideoDataResultBlock)callBack{
    NSMutableArray *parameters = [NSMutableArray array];
    for (int i = 1; i <= 15; i++) {
      NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"mediaPath"] = [NSString stringWithFormat:@"aface/lanlin/AquaPolo/vedio/%d.mp4",i];
        dict[@"mediaType"] = @"video";
      [parameters addObject:dict];
    }
    [APDataManager postJsonWithUrlString:mediaSearch timeoutInterval:0 parameters:parameters result:^(NSDictionary * _Nullable res) {
        NSArray *datas = res[@"data"];
        if ([datas isKindOfClass:[NSArray class]]) {
            for (int i = 0; i < datas.count; i++) {
                APBroadcasterModel *userModel = users[i];
                APVideoModel *videoModel = [APVideoModel yy_modelWithDictionary:datas[i]];
                videoModel.userId = userModel.userId;
                videoModel.avatar = userModel.avatar;
                videoModel.nickname = userModel.nickname;
                videoModel.title = self.titles[i];
                videoModel.isUpvote = false;
                videoModel.isFollow = false;
                videoModel.isBlack = false;
                [self.videosList addObject:videoModel];
            }
            callBack(self.videosList);
        }else{
            callBack(@[]);
        }
    } failBlock:^(NSString * _Nullable errorMsg) {
        if (callBack) {
            callBack(@[]);
        }
    }];
    
}

- (void)clearData{
    [self.videosList removeAllObjects];
    [APVideoDataManager removeFile:video_file];
}

@end
